﻿// -------------------------------------------------------------------------
//    @FileName         :    NFTransAccLogin.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-08-21
//    @Email			:    445267987@qq.com
//    @Module           :    NFTransAccLogin
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFTransAccLogin.h"

#include "ConstantDescEx.h"
#include "NFOnlineAccMgr.h"
#include "NFOnlineAcc.h"
#include "NFTransLoadAcc.h"
#include "NFLogicCommon/NFBaseDefine.h"

typedef enum
{
    EN_ACC_LOGIN_INIT = 0,
    EN_ACC_LOGIN_GET_ROLE_LIST = 1,
} ETransGetRoleListState;

NFTransAccLogin::NFTransAccLogin()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFTransAccLogin::~NFTransAccLogin()
{
}

int NFTransAccLogin::CreateInit()
{
    SetState(EN_ACC_LOGIN_INIT);
    m_iServerType = NF_ST_ONLINE_SERVER;
    m_fromBusId = 0;
    m_transId = 0;
    m_zoneId = 0;
    m_playerUin = 0;
    m_platformType = 0;
    return 0;
}

int NFTransAccLogin::ResumeInit()
{
    return 0;
}

int NFTransAccLogin::StartGetRoleList(int zoneId, uint64_t playerUin, int platformType, int fromBusId, int transId)
{
    CHECK_EXPR(GetState() == EN_ACC_LOGIN_INIT, proto_ff::ERR_CODE_SYSTEM_OBJSTATUS_INVALID, "invalid tran state! {}", DebugString());
    m_zoneId = zoneId;
    m_playerUin = playerUin;
    m_ullRoleId = playerUin;
    m_platformType = platformType;
    m_fromBusId = fromBusId;
    m_transId = transId;

    proto_ff::tbRoleList tbRoleList;
    tbRoleList.set_uin(m_playerUin);
    tbRoleList.set_world_id(m_zoneId);
    tbRoleList.set_world_type(m_platformType);
    GetCacheDataSingleReq<NFTransLoadAcc>(m_playerUin, 0, tbRoleList, E_TABLE_ROLE_LIST);

    SetState(EN_ACC_LOGIN_GET_ROLE_LIST);
    return 0;
}

int NFTransAccLogin::GetCacheDataRes(int ullId, int iTableId, int iReason, int32_t errCode, google::protobuf::Message* pMessage)
{
    CHECK_EXPR(GetState() == EN_ACC_LOGIN_GET_ROLE_LIST, proto_ff::ERR_CODE_SYSTEM_OBJSTATUS_INVALID, "invalid tran state! {}", DebugString());

    auto pOnlineAcc = NFOnlineAccMgr::Instance()->FindOnlineAcc(m_zoneId, m_playerUin);
    CHECK_NULL_WF(0, pOnlineAcc, "FindOnlineAcc failed, zoneId: {}, playerUin: {}", m_zoneId, m_playerUin);

    if (errCode == ERR_CODE_STORESVR_ERRCODE_SELECT_EMPTY)
    {
        proto_ff::tbRoleList tbRoleList;
        MakeNewRoleList(tbRoleList);
        pOnlineAcc->InitData(&tbRoleList);
        pOnlineAcc->SetInitRoleList();
        pOnlineAcc->SendRoleList(m_fromBusId, m_transId);
        SetFinished(0);
        return 0;
    }
    else if (errCode != 0)
    {
        LOG_ERR(ullId, errCode, "GetCacheDataRes failed, errCode: {}", errCode);
        return errCode;
    }
    else
    {
        proto_ff::tbRoleList* pRoleList = dynamic_cast<proto_ff::tbRoleList*>(pMessage);
        CHECK_NULL_WF(ullId, pRoleList, "dynamic_cast<tbRoleList> failed, roleId: {}", ullId);
        pOnlineAcc->InitData(pRoleList);
        pOnlineAcc->SetInitRoleList();
        pOnlineAcc->SendRoleList(m_fromBusId, m_transId);
        SetFinished(0);
        return 0;
    }
    return 0;
}

int NFTransAccLogin::MakeNewRoleList(proto_ff::tbRoleList& tbRoleList)
{
    tbRoleList.set_uin(m_playerUin);
    tbRoleList.set_world_id(m_zoneId);
    tbRoleList.set_world_type(m_platformType);
    tbRoleList.set_guard_start_time(0);
    tbRoleList.set_guard_end_time(0);
    tbRoleList.set_guard_msg("");

    tbRoleList.mutable_storage_data()->set_max_grids(ConstantDescEx::Instance()->GetValue(proto_ff::EN_CONST_STORAGE_PAG_NUM_OF_BRON));
    tbRoleList.mutable_storage_data()->set_buy_grids_times(0);
    return 0;
}
